package com.trussell.genesis.windowutils.toolbars;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import com.trussell.genesis.GenesisImplementor;
import com.trussell.genesis.GenesisImplementor.Mode;
import com.trussell.genesis.windowutils.listeners.ModeActionListener;
import com.trussell.genesis.windowutils.listeners.NewWorldActionListener;
import com.trussell.genesis.windowutils.listeners.SelectWorkspaceActionListener;

/**
 * This toolbar is used to determine the mode of operation
 * of the editor. This includes World Edit, Tile Edit, and
 * Effects Edit.
 * 
 * @author Tyler
 *
 */
public class ModeToolBar extends JToolBar {
	
	private static final long serialVersionUID = 1L;
	
	private JToggleButton worldEditorMenuItem = new JToggleButton();
	private JToggleButton terrainMenuItem = new JToggleButton();
	private JToggleButton effectsMenuItem = new JToggleButton();

	public ModeToolBar() {
		
		setFloatable(false);
		
		/*
		 * Prepare buttons
		 */
		ButtonGroup bg = new ButtonGroup();
		JButton selectWorkspace = new JButton();
		JButton newFile = new JButton();
		JButton openFile = new JButton();
		JButton saveFile = new JButton();
		JButton export = new JButton();
		
		JButton cut = new JButton();
		JButton copy = new JButton();
		JButton paste = new JButton();
		
		bg.add(worldEditorMenuItem);
		bg.add(terrainMenuItem);
		bg.add(effectsMenuItem);
		worldEditorMenuItem.setSelected(true);
		
		/*
		 * Configure icons and text.
		 */
		newFile.setIcon(new ImageIcon("images/basicicons/New24.gif"));
		newFile.setToolTipText("New World");
		openFile.setIcon(new ImageIcon("images/basicicons/Open24.gif"));
		openFile.setToolTipText("Open World");
		saveFile.setIcon(new ImageIcon("images/basicicons/Save24.gif"));
		saveFile.setToolTipText("Save World");
		export.setIcon(new ImageIcon("images/basicicons/Export24.gif"));
		export.setToolTipText("Export for Game Engine");
		cut.setIcon(new ImageIcon("images/basicicons/Cut24.gif"));
		cut.setToolTipText("Cut");
		copy.setIcon(new ImageIcon("images/basicicons/Copy24.gif"));
		copy.setToolTipText("Copy");
		paste.setIcon(new ImageIcon("images/basicicons/Paste24.gif"));
		paste.setToolTipText("Paste");
		selectWorkspace.setIcon(new ImageIcon("images/basicicons/Preferences24.gif"));
		selectWorkspace.setToolTipText("Select Workspace");
		
		worldEditorMenuItem.setIcon(new ImageIcon("images/world_editor.png"));
		worldEditorMenuItem.addActionListener(new ModeActionListener(Mode.NONE));
		worldEditorMenuItem.setToolTipText(
				"<html><b><font size=4>World Editor</b></font size><br>" +
				"Use this mode to arrange the Tiles<br>" +
				"of your world into a grid, or to add<br>" +
				"new tiles into the world.");
		
		terrainMenuItem.setIcon(new ImageIcon("images/environment.png"));
		terrainMenuItem.addActionListener(new ModeActionListener(Mode.TERRAIN_EDIT));
		terrainMenuItem.setToolTipText(
				"<html><b><font size=4>Tile Editor</b></font size><br>" +
				"Use this mode to mold specific tiles<br>" +
				"of terrain.");
		
		effectsMenuItem.setIcon(new ImageIcon("images/effects.png"));
		effectsMenuItem.addActionListener(new ModeActionListener(Mode.EFFECTS_EDIT));
		effectsMenuItem.setToolTipText(
				"<html><b><font size=4>Effects Editor</b></font size><br>" +
				"Use this mode to set environmental<br>" +
				"effects like rain, snow, shadows<br>" +
				"bloom, or others.");
		
		selectWorkspace.addActionListener(new SelectWorkspaceActionListener());
		newFile.addActionListener(new NewWorldActionListener());
		
		/*
		 * Add JMenuItem(s) to toolbar.
		 */
		add(selectWorkspace);
		addSeparator();
		add(newFile);
		add(openFile);
		add(saveFile);
		add(export);
		addSeparator();
		add(cut);
		add(copy);
		add(paste);
		addSeparator();
		add(worldEditorMenuItem);
		add(terrainMenuItem);
		add(effectsMenuItem);
	}
	
	public void setMode(Mode mode) {
		switch(mode) {
		case NONE:
			worldEditorMenuItem.setSelected(true);
			GenesisImplementor.getInstance().setMode(Mode.NONE, false);
			break;
		case TERRAIN_EDIT:
			terrainMenuItem.setSelected(true);
			GenesisImplementor.getInstance().setMode(Mode.TERRAIN_EDIT, false);
			break;
		case EFFECTS_EDIT:
			effectsMenuItem.setSelected(true);
			GenesisImplementor.getInstance().setMode(Mode.EFFECTS_EDIT, false);
			break;
		}
	}
}
